/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * Main.java
 * Created on January 20, 2008, 10:48 AM
 */

package tictionary;

import java.util.List;
import java.util.ListIterator;
import tictionary.dict.Dict;
import tictionary.dict.DictCreator;
import tictionary.dict.Letter;
import tictionary.dict.LetterSet;
import tictionary.dict.OffsetComputer;
import tictionary.dict.Serializer;

/**
 *
 * @author Erol Hira
 */
public class DictMaker {
    
    public DictMaker() {
        generateDicts();
    }
    
    
    public static void main(String[] args) {        
        
        new DictMaker();
        
        System.out.println("done");
    }
    
    public void generateDicts(){
        
        Dict dict = new DictCreator("db.txt").createDict();
        
        List<Dict> dicts = dict.split();
        
        for(Dict d : dicts){
            d.compress();
        }
        
        for(Dict d : dicts){
            generateDict(d);
        }
        
        for(Dict d : dicts){
            testDict(d.getRootSet());
        }
    }
    
    public void generateDict(Dict dict){
        
        OffsetComputer utils = new OffsetComputer(dict);
        
        utils.calculateOffsets();
        
        Serializer serializer = new Serializer(dict);
        serializer.serialize();
    }

    
    StringBuffer sbuf = new StringBuffer();
    private void testDict(LetterSet letterSet) {                                        
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            if(letter.getChildSet() == null){                
                sbuf.append(letter.getChars());
                System.out.println(sbuf.toString());
                sbuf.setLength(sbuf.toString().length()-letter.getChars().length());
                
            } else {
                sbuf.append(letter.getLetter());
                testDict(letter.getChildSet());
                if(letter.getChildSet() != null){
                    sbuf.setLength(sbuf.length() - 1);
                }
            }                        
        }
    }
    
}
